package com.distribute.sso.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.distribute.common.pojo.DistributeResult;
import com.distribute.common.util.ExceptionUtil;
import com.distribute.pojo.DbUser;
import com.distribute.sso.service.UserService;
import com.sun.tools.internal.xjc.generator.bean.ImplStructureStrategy.Result;

/**
 * 用户Controller
 * <p>
 * Title: UserController
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company: www.mings.com
 * </p>
 * 
 * @author 曾晓明
 * @date 2018年01月14日上午10:52:43
 * @version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/check/{param}/{type}")
	@ResponseBody
	public Object checkData(@PathVariable String param, @PathVariable Integer type, String callback) {

		DistributeResult result = null;

		// 参数有效性校验
		if (StringUtils.isBlank(param)) {
			result = DistributeResult.build(400, "校验内容不能为空");
		}
		if (type == null) {
			result = DistributeResult.build(400, "校验内容类型不能为空");
		}
		if (type != 1 && type != 2 && type != 3) {
			result = DistributeResult.build(400, "校验内容类型错误");
		}
		// 校验出错
		if (null != result) {
			if (null != callback) {
				MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
				mappingJacksonValue.setJsonpFunction(callback);
				return mappingJacksonValue;
			} else {
				return result;
			}
		}
		// 调用服务
		try {
			result = userService.checkData(param, type);

		} catch (Exception e) {
			result = DistributeResult.build(500, ExceptionUtil.getStackTrace(e));
		}

		if (null != callback) {
			MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
			mappingJacksonValue.setJsonpFunction(callback);
			return mappingJacksonValue;
		} else {
			return result;
		}
	}

	// 创建用户
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public DistributeResult createUser(DbUser user) {

		try {
			DistributeResult result = userService.createUser(user);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return DistributeResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}

	// 用户登录
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public DistributeResult userLogin(String username, String password, HttpServletRequest request,
			HttpServletResponse response) {
		try {

			DistributeResult result = userService.userLogin(username.trim(), password.trim(), request, response);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return DistributeResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}

	// 用户注销
	@RequestMapping(value = "/logout/{token}")
	@ResponseBody
	public Object userLogout(@PathVariable String token, String callback) {
		DistributeResult result = null;
		try {

			result = userService.userLogout(token);
		} catch (Exception e) {
			e.printStackTrace();
			result = DistributeResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		if (StringUtils.isBlank(callback)) {
			return result;
		} else {
			MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
			mappingJacksonValue.setJsonpFunction(callback);
			return mappingJacksonValue;
		}
	}

	@RequestMapping("/token/{token}")
	@ResponseBody
	public Object getUserByToken(@PathVariable String token, String callback) {
		DistributeResult result = null;
		try {
			result = userService.getUserByToken(token);
		} catch (Exception e) {
			e.printStackTrace();
			result = DistributeResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		System.out.println(callback + "ccccccccccccccccccccccccccccccccccccccccccccc");
		// 判断是否为jsonp调用
		if (StringUtils.isBlank(callback)) {
			return result;
		} else {
			MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
			mappingJacksonValue.setJsonpFunction(callback);
			return mappingJacksonValue;
		}
	}

	@RequestMapping(value="/checkPass",method=RequestMethod.POST)
	@ResponseBody
	public DistributeResult checkPass(String userName, String password) {
		try {
			boolean flag = userService.checkUserPass(userName.trim(), password.trim());
			if (flag) {
				return DistributeResult.ok();
			} else {
				return DistributeResult.build(500, "error");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return DistributeResult.build(500, "error");
		}
	}

	@RequestMapping(value="/change/userInfo" ,method=RequestMethod.POST)
	@ResponseBody
	public DistributeResult changeUserInfo(String userName, String phone, String email,String token) {
		try {
			DistributeResult result = userService.updateUserInfo(userName.trim(), phone.trim(), email.trim(),token);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return DistributeResult.build(500, "error");
		}
	}

	@RequestMapping(value="/change/userPass",method=RequestMethod.POST)
	@ResponseBody
	public DistributeResult changeUserPass(String userName, String password,String token) {
		try {
			DistributeResult result = userService.updateUserPass(userName.trim(), password.trim(),token);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return DistributeResult.build(500, "error");
		}
	}
}